package cn.charlotte.pit.commands.player;


import cn.charlotte.pit.ThePit;
import cn.charlotte.pit.UtilKt;
import cn.charlotte.pit.commands.PitCommands;
import cn.charlotte.pit.data.PlayerProfile;
import cn.charlotte.pit.menu.viewer.StatusViewerMenu;
import cn.charlotte.pit.util.chat.CC;
import cn.charlotte.pit.util.level.LevelUtil;
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.Default;
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

@CommandAlias("view|look")
public class View extends BaseCommand {
    private final PitCommands pitCommands;

    public View(PitCommands pitCommands) {
        this.pitCommands = pitCommands;
    }

    @Default
    @CommandCompletion("@players")
    public void viewProfile(Player player, OnlinePlayer target) {
        final Long present = this.pitCommands.viewCooldown.getIfPresent(player.getUniqueId());
        if (present != null) {
            player.sendMessage(CC.translate("&c冷却中..."));
            return;
        }

        this.pitCommands.viewCooldown.put(player.getUniqueId(), System.currentTimeMillis());

        try {
            PlayerProfile profile = PlayerProfile.getPlayerProfileByUuid(player.getUniqueId());
            if (profile.getLevel() < 70 && profile.getPrestige() < 1) {
                player.sendMessage(CC.translate("&c&l等级不足! &7此指令在 " + LevelUtil.getLevelTag(profile.getPrestige(), 70) + " &7时解锁."));
                return;
            }
            Bukkit.getScheduler().runTaskAsynchronously(ThePit.getInstance(), () -> {
                PlayerProfile targetProfile = UtilKt.getPitProfile(target.player);
                if (targetProfile == null) {
                    player.sendMessage(CC.translate("&c此玩家的档案不存在,请检查输入是否有误."));
                    return;
                }
                Bukkit.getScheduler().runTask(ThePit.getInstance(), () -> new StatusViewerMenu(targetProfile).openMenu(player));
            });

        } catch (Exception e) {
            if (player.hasPermission("pit.admin")) {
                CC.printError(player, e);
            }
        }
    }

}
